import 'package:hb_password_input_textfield/hb_password_input_textfield.dart';
import 'package:flutter/material.dart';
import 'package:fluttertoast/fluttertoast.dart';
import 'package:shop/http/HttpUtil.dart';
import 'package:shop/route/my_route.dart';
import 'package:shop/shop/life/Index.dart';
import 'package:shop/shop/life/OrderListPage.dart';
class PayMima extends StatefulWidget {
  String unionid;//联合订单ID
  String userId;//用户ID
  double money;//支付金额
  int type;//1：立即购买页面支付  2：未付款订单页面支付  3：全部订单页面支付  4：外卖支付
  PayMima({Key key, this.title,this.unionid,this.userId,this.money,this.type}) : super(key: key);

  final String title;

  @override
  _PayMima createState() => _PayMima(unionid,userId,money,type);
}

class _PayMima extends State<PayMima> {

  String unionid="";//联合订单ID
  String userId="";//用户ID
  double money=0.00;//支付金额
  String payPwd="";//支付密码
  int type;
  // 构造方法
  _PayMima(unionid,userId,money,type){
    this.unionid=unionid;
    this.userId=userId;
    this.money=money;
    this.type=type;
  }

  _CheckPayPwd() async {
    var x = await HttpUtil().get("/app/orderPay/checkPayPwd?payPwd="+payPwd+"&userId="+userId);
    if(x['code']==200){//密码正确
      if(type==4){
        var y = await HttpUtil().get("/waimai/mOrderPay/jifenPay?ddhao="+unionid+"&userId="+userId+"&jifen=${money}");
        if(y['code']==200){//支付成功
          Fluttertoast.showToast(
              msg: "支付成功",
              toastLength: Toast.LENGTH_SHORT,
              gravity: ToastGravity.BOTTOM,
              timeInSecForIosWeb: 1,
              backgroundColor: Colors.black45,
              textColor: Colors.white,
              fontSize: 16.0
          );
          //延时2s执行返回
          Future.delayed(Duration(seconds: 1), (){
            MyRoute.replace(Index());
          });
        }else{
          Fluttertoast.showToast(
              msg: y['msg'],
              toastLength: Toast.LENGTH_SHORT,
              gravity: ToastGravity.BOTTOM,
              timeInSecForIosWeb: 1,
              backgroundColor: Colors.black45,
              textColor: Colors.white,
              fontSize: 16.0
          );
        }
      }else{
        var y = await HttpUtil().get("/app/orderPay/jifenPay?ddhao="+unionid+"&userId="+userId+"&jifen=${money}");
        if(y['code']==200){//支付成功
          Fluttertoast.showToast(
              msg: "支付成功"
          );
          //延时2s执行返回
          Future.delayed(Duration(seconds: 2), (){
            if(type==1){
              //支付成功跳首页或个人中心
              MyRoute.replace(Index());
            }else if(type==2){
              MyRoute.pop('1');
            }else if(type==3){
              MyRoute.pop(OrderListPage(showIndex:'0'));
            }
          });
        }else{
          Fluttertoast.showToast(
              msg: y['msg']
          );
        }
      }
    }else{
      Fluttertoast.showToast(
          msg: x['msg']
      );
    }
  }

  @override
  Widget build(BuildContext context) {

    TextEditingController _controller = TextEditingController();
    FocusNode _node = FocusNode();

    return Scaffold(
      appBar: AppBar(
        // Here we take the value from the MyHomePage object that was created by
        // the App.build method, and use it to set our appbar title.
        title: Text("输入支付密码"),
      ),
      body:Column(children: [
        Container(child: Text("支付金额：${money}"),),
        GestureDetector(
            behavior: HitTestBehavior.opaque,
            onTap: () {
              _node.unfocus();
            },
            child: Container(
              margin: EdgeInsets.only(top: 50),
              child: HBPasswordInputTextField(
                // backgroundColor: Colors.red,
                // fillColor: Colors.red,
                borderWidth: 0.5,
                borderRaiuds: 5,
                controller: _controller,
                node: _node,
                // backgroundColor: Colors.lightBlue,
                obscureText: true,
                // obscureTextString: "🤪",
                boxWidth: 50,
                boxHeight: 50,
                type: HBPasswordInputTextFieldType.SEGMENTED,
                length: 6,
                textStyle: TextStyle(fontSize: 20),
                onChange: (text) {
                  if(text.toString().length==6){
                    payPwd = text;
                    _controller.text = "";
                    _CheckPayPwd();
                  }
                  // 如果密码输入完成，接下来判断密码是否正确
                  // 如果密码错误提示重新输入还是重置密码
                },
              ),
            )),
      ],)

    );
  }
}